﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ADShop.Models;
using ADShop.Repository;
namespace ADShop.Controllers
{
    public class MemberController : Controller
    {
        //private ADShopEntities db = new ADShopEntities();
        private EShopEntities1 db = new EShopEntities1();

        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View(db.Member.ToList());
        }

        //
        // GET: /Admin/Details/5

        public ActionResult Details(int id = 0)
        {
            Member member = db.Member.Find(id);
            ViewBag.InfoMember = db.InfoMember.FirstOrDefault(d => d.UserID == id);
            if (member == null)
            {
                return HttpNotFound();
            }
            return View(member);
        }

        //
        // GET: /Admin/Create

        public ActionResult Create()
        {
            ViewBag.ListPrivilege = new SelectList(db.Privilege.ToList(), "IDPrivilege", "NamePrivilege", 0);
            return View();
        }

        //
        // POST: /Admin/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Member member,FormCollection fm)
        {
            var Result = db.Member.FirstOrDefault(d => d.UserEmail == member.UserEmail);
            if (Result == null)
            {
                if (ModelState.IsValid)
                {
                    //Thêm Tài Khoản Mới
                    Member mb = new Member();
                    mb.UserEmail = member.UserEmail;
                    mb.Password = Global.MD5(member.Password);
                    mb.Active = member.Active;
                    mb.IDPrivilege = member.IDPrivilege;
                    db.Member.Add(mb);
                    db.SaveChanges();
                    //Kết Thúc Thêm Tài Khoản Mới
                    //Thêm Thông Tin Cho Tài Khoản
                    InfoMember im = new InfoMember();
                    im.UserID = db.Member.FirstOrDefault(d => d.UserEmail == member.UserEmail).IDUser;
                    im.Fax = fm["tbFax"];
                    im.NumberPhone = fm["tbNumberPhone"];
                    im.NumberCardMember = fm["tbNumberCardMember"];
                    im.Company = fm["tbCompany"];
                    im.Representative = fm["tbRepresentative"];
                    im.Address = fm["tbAddressCompany"];
                    im.TaxCode = fm["tbTaxCode"];
                    db.InfoMember.Add(im);
                    db.SaveChanges();
                    //Kết Thúc Thêm Thông Tin Cho Tài Khoản
                    return RedirectToAction("Index");
                }
            }
            ViewBag.TaiKhoanDaTonTai = "Email is current use ! Please select other email :)";
            return View(member);
        }

        //
        // GET: /Admin/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Member admin = db.Member.Find(id);
            ViewBag.InfoMember = db.InfoMember.FirstOrDefault(d => d.UserID == id);
            ViewBag.ListPrivilege = new SelectList(db.Privilege.ToList(), "IDPrivilege", "NamePrivilege", admin.IDPrivilege);
            if (admin == null)
            {
                return HttpNotFound();
            }
            return View(admin);
        }

        //
        // POST: /Admin/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Member member)
        {
            if (ModelState.IsValid)
            {
                db.Entry(member).State = EntityState.Modified;
                UpdateModel(member);
                //Thêm Thông Tin Cho Tài Khoản
                //InfoMember im = new InfoMember();
                //im.UserID = member.UserID;
                //im.Fax = fc["tbFax"];
                //im.NumberPhone = fc["tbNumberPhone"];
                //im.NumberCardMember = fc["tbNumberCardMember"];
                //im.Company = fc["tbCompany"];
                //im.Representative = fc["tbRepresentative"];
                //im.AddressCompany = fc["tbAddressCompany"];
                //im.TaxCode = fc["tbTaxCode"];
                //db.Entry(im).State = EntityState.Modified;
                db.SaveChanges();
                //Kết Thúc Thêm Thông Tin Cho Tài Khoản
                return RedirectToAction("Index");
            }
            return View(member);
        }

        //
        // GET: /Admin/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Member admin = db.Member.Find(id);
            db.Member.Remove(admin);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}